package com.groupThree.controller;

import com.groupThree.bean.*;
import com.groupThree.mvc.ResponseBody;
import com.groupThree.service.UserService;
import com.groupThree.util.DateFormatUtil;
import com.groupThree.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {
    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = UserService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String u_name = request.getParameter("u_name");
        String u_phone = request.getParameter("u_phone");
        String u_idcard = request.getParameter("u_idcard");
        String u_password = request.getParameter("u_password");

        User u = new User(u_name, u_phone, u_idcard, u_password);
        Message msg = new Message();
        StateOfAU flag = UserService.insert(u);
        int sta = flag.getStateAu();
        switch (sta){
            case 0:  //成功
                msg.setStatus(0);
                msg.setResult("录入成功！");
                break;
            case 1:
                msg.setStatus(-1);
                msg.setResult("电话号码已经存在！");
                break;
            case 2:
                msg.setStatus(-1);
                msg.setResult("身份证号码已经存在！");
                break;
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<User> list = UserService.findAll(true, offset, pageNumber);

        List<BootStrapTableUser> list2 = new ArrayList<>();
        List<Map<String,Integer>> console = UserService.console();
        for (User user: list){
            String regristertime = DateFormatUtil.format(user.getU_registertime());
            String logintime = user.getU_logintime()==null?regristertime : DateFormatUtil.format(user.getU_logintime());
            int size = user.getU_password().length();
            String v_password = "*";
            String u_password = "";
            for (int i= 0;i<size;i++){
                u_password = v_password+u_password;
            }
            BootStrapTableUser user2 = new BootStrapTableUser(user.getU_id(),user.getU_name(),user.getU_phone(),user.getU_idcard(),u_password,regristertime,logintime);
            list2.add(user2);
        }
        Integer total = console.get(0).get("data_size");

        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String u_phone = request.getParameter("u_phone");
        User user = UserService.findByU_Phone(u_phone);
        Message msg = new Message();
        if (user.getU_name()== null) {
            msg.setStatus(-1);
            msg.setResult("该手机号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);

        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("u_id"));
        boolean flag = UserService.delete(id);
        Message msg = new Message();

        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }

        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("u_id"));
        String name = request.getParameter("u_name");
        String phone = request.getParameter("u_phone");
        String idcard = request.getParameter("u_idcard");
        String password = request.getParameter("u_password");

      User newUser = new User();

        newUser.setU_name(name);
        newUser.setU_phone(phone);
        newUser.setU_idcard(idcard);
        newUser.setU_password(password);
        StateOfAU flag = UserService.update(id, newUser);

        Message msg = new Message();
        int sta = flag.getStateAu();
        switch (sta){
            case 0:  //成功
                msg.setStatus(0);
                msg.setResult("修改成功！");
                break;
            case 1:
                msg.setStatus(-1);
                msg.setResult("电话号码已经存在！");
                break;
            case 2:
                msg.setStatus(-1);
                msg.setResult("身份证号码已经存在！");
                break;
            case -1:
                msg.setStatus(-1);
                msg.setResult("修改失败");
        }


        String json = JSONUtil.toJSON(msg);
        return json;


    }
}
